package com.netdisk.controller;
/**
 * @author chenlong
 * 登录acition
 * 
 * 2013-5-17 22:46:40
 */
import static com.netdisk.util.UUIDCreator.getUuId;
import static com.netdisk.util.RemoteAddress.getRemoteAddress;
import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static com.netdisk.util.EncrypSHA.eccrypt;

import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.impl.LogService;
import com.netdisk.service.impl.UserService;
import com.netdisk.vo.LoginInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport implements ModelDriven {

	private LoginInfo loginInfo = new LoginInfo();

	private User user;

	private LogService logService;
	
	private UserService userService;

	public LoginInfo getLoginInfo() {
		return loginInfo;
	}

	@Override
	public Object getModel() {
		return loginInfo;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setLoginInfo(LoginInfo loginInfo) {
		this.loginInfo = loginInfo;
	}

	@Resource(name="userService")
	public void setUserService(UserService userService) {
		System.out.println(userService.getClass());
		this.userService = userService;
	}
	
	@Resource(name="logService")
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println(loginInfo.toString());
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		user = userService.findByLoginName(loginInfo.getLoginName());
		if(null == user){
			addFieldError("loginNameErro", getText("loginNameErro"));
			return INPUT;
		}
		String p1 = user.getLoginPassword(); // 数据库里面的密码
		String p2 = eccrypt(loginInfo.getLoginPassword());// 用户输入的密码
		Object logined = session.getAttribute("logined");
		String random = (String) session.getAttribute("random");
		System.out.println(request.getCookies());
		System.out.println(random);
		//验证码的验证
		if (!random.equalsIgnoreCase(loginInfo.getRandomStr())) {
			addFieldError("randomConfirmErro",getText("randomConfirmErro"));
			System.out.println("random confirm erro");
			return INPUT;
		}
		
		// 如果已经登录就直接调整到主页面, 否则把用户的密码从数据库里面的密码比较.
		System.out.println(p1 + "    " + p2);
		if ( (logined != null ) || (null != user && p1.equals(p2))) {
			session.setAttribute("logined",true);
			Log log = null;//登录日志
			
			System.out.println(user.getCompanyId());
			
			if(user.getCompanyId() != null){//企业用户登录日志
				log = new Log(getUuId(), user.getCompanyId(), user.getCompanyName(),user.getId(), 
					user.getLoginName(), (byte)21,  Calendar.getInstance().getTimeInMillis(), getRemoteAddress(request));
			}else{//个人用户登录日志
				log = new Log(getUuId(), (byte)1, user.getId(), user.getLoginName(),
						 Calendar.getInstance().getTimeInMillis(), getRemoteAddress(request));
			}
				
			logService.save(log);
			if(null != loginInfo.getRememberMe()){
				Cookie n = new Cookie("loginName", user.getLoginName());
				//为了防止伪造cookie, 这里不能保存加密后的密码,保存时用户这次登录的日志id, 
				Cookie p = new Cookie("loginPassword", log.getId());
				HttpServletResponse response = ServletActionContext.getResponse();
				response.addCookie(n);
				response.addCookie(p);
				n.setMaxAge(60 * 60 * 24 * 7);//秒  分  时 天, 默认一个星期过期
				p.setMaxAge(60 * 60 * 24 * 7); 
				System.out.println("save cookie");
			}
			return SUCCESS;
		}
		return INPUT;
	}
}
